package com.mf.controller.createdThread;

import com.mf.controller.createdThread.CreatedThreadUtil.CreatedThreadMyJob;
import com.mf.result.Result;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

/**
 * @Author: lmf
 * @Create: 2025/5/12 20:56
 * @module 线程创建的三种方式
 */
@RestController
@RequestMapping("createdThread")
public class CreatedThread {

    @Operation(summary = "继承Thread类重新run方法【不常用】")
    @GetMapping("extendsThread")
    public Result<String> extendsThread() {
        final CreatedThreadMyJob createdThreadMyJob = new CreatedThreadMyJob();
        createdThreadMyJob.start();
        for (int i = 0; i < 100; i++) {
            System.out.println("main thread: " + i);
        }
        System.out.println("main thread end");
        return Result.success("查看查询信息");
    }

    @Operation(summary = "实现Runnable接口重写run方法-使用lambda方式【常用】")
    @GetMapping("implementsRunnableByLambda")
    public Result<String> extendRunnableByLambda() {
        final Thread thread = new Thread(() -> {
            System.out.println("runnable线程开始");
            for (int i = 0; i < 100; i++) {
                System.out.println("main thread: " + i);
            }
        });
        thread.start();
        System.out.println("主线程开始");
        for (int i = 0; i < 100; i++) {
            System.out.println("主线程: " + i);
        }
        return Result.success("查看查询信息");
    }

    @Operation(summary = "实现Runnable接口，重写run方法-使用匿名内部类【常用】")
    @GetMapping("implementsRunnableByClass")
    public Result<String> implementsRunnableByClass() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                System.out.println("runnable线程开始");
                for (int i = 0; i < 100; i++) {
                    System.out.println("main thread: " + i);
                }
            }
        }).start();
        System.out.println("主线程开始");
        for (int i = 0; i < 100; i++) {
            System.out.println("主线程: " + i);
        }
        return Result.success("查看查询信息");
    }

    @Operation(summary = "实现Callable重写Call方法，配合FutureTask方法 【有返回结果】")
    @GetMapping("implementsCallable")
    public Result<Integer> implementsCallable() throws InterruptedException, ExecutionException {
        // 创建futureTask任务，传入Callable接口实现类
        final FutureTask<Integer> futureTask = new FutureTask<Integer>(() -> {
            System.out.println("Callable线程开始");
            int count = 0;
            for (int i = 0; i < 100; i++) {
                System.out.println("main thread: " + i);
                count++;
            }
            return count;
        });
        // 启动线程
        new Thread(futureTask).start();

        // 做一些其他操作
        Thread.sleep(1000);

        // 等待操作结果
        final Integer i = futureTask.get();


        return Result.success(i);
    }


}
